package com.androidwwf.jwr;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class GameQuiz extends Activity{

	private Button bCheckAnswer;
	private RadioGroup rg;
	private TextView tvQuestion;
	private RadioButton rAnswer1;
	private RadioButton rAnswer2;
	private RadioButton rAnswer3;
	private RadioButton rAnswer4;
	private DBQuiz dbquiz;
	private int correctAnswer;
	private Dialog d;
	private TextView x;
	private String[] newquestion;
	private int questionnumber = 0;
	private int totalPoints = 0;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamequiz);  
        init();
        setControls();
    }
	
	private void init()
	{
		bCheckAnswer = (Button) findViewById(R.id.bQuizCheckAnswer);
		bCheckAnswer.setOnClickListener(new buttonCheckAnswer());
		
		tvQuestion = (TextView) findViewById(R.id.tvQuestion);
		rAnswer1 = (RadioButton) findViewById(R.id.rAnswer1);
		rAnswer2 = (RadioButton) findViewById(R.id.rAnswer2);
		rAnswer3 = (RadioButton) findViewById(R.id.rAnswer3);
		rAnswer4 = (RadioButton) findViewById(R.id.rAnswer4);
		rg = (RadioGroup) findViewById(R.id.rgAnswers);
		dbquiz = new DBQuiz(this);
		d = new Dialog(this);
		d.setOnCancelListener(new OnCancelListener() {
			
			public void onCancel(DialogInterface dialog) {
				
				int selectedbutton = getCheckedRadio();
				if(selectedbutton != 0)
				{
					setControls();
				}
			}
		});

		x = new TextView(this);	
	}
	
	private void setControls()
	{
		if(questionnumber >= 0 && questionnumber < 5)
		{
			if(!dbquiz.checkIfEmpty())
			{
				dbquiz.open();
				this.newquestion = dbquiz.getNewQuestion();
				dbquiz.close();
				
				tvQuestion.setText(newquestion[1]);
				rAnswer1.setText(newquestion[2]);
				rAnswer2.setText(newquestion[3]);
				rAnswer3.setText(newquestion[4]);
				rAnswer4.setText(newquestion[5]);
				correctAnswer = Integer.parseInt(newquestion[6]);
				deselectButtonsInRadioGroup();
				questionnumber++;
			}
			else
			{
				setControls();
			}
		}
		else
		{
			Intent gameResult = new Intent("com.androidwwf.jwr.GAMERESULT");

			gameResult.putExtra("typegame", "quiz");
			gameResult.putExtra("points", totalPoints);
			
			questionnumber = 0;
			totalPoints = 0;
			finish();
			
			startActivity(gameResult);
		}
	}
	
	private void deselectButtonsInRadioGroup()
	{
		rg.clearCheck();
	}
	
	private class buttonCheckAnswer implements OnClickListener
	{
		public void onClick(View v) {
			int selectedbutton = getCheckedRadio();
			
			if(selectedbutton == correctAnswer)
			{
				x.setText("Proceed to the next question.");		
				d.setContentView(x);
				d.setTitle("Congratulations. Correct Answer");
				d.show();
				totalPoints += Integer.parseInt(newquestion[7]);
			}
			else if(selectedbutton == 0)
			{
				x.setText("No answer is given. Please select a answer.");	
				d.setContentView(x);
				d.setTitle("Select a answer!");
				d.show();
			}
			else
			{
				x.setText("Proceed to the next question.");	
				d.setContentView(x);
				d.setTitle("Sorry. Incorrect Answer.");
				d.show();	
			}
		}
	}
	
	private int getCheckedRadio()
	{
		int button = 0;
		
		if(rAnswer1.isChecked())
		{
			button =  1;
		}
		else if(rAnswer2.isChecked())
		{
			button = 2;
		}
		else if(rAnswer3.isChecked())
		{
			button = 3;
		}
		else if(rAnswer4.isChecked())
		{
			button = 4;
		}
		return button;
	}
}
